var baseConfig = require("../../baseConfig")
var utils = require("../../utils/utils")
Page({
  data: {
    index: 0,
    nameArray: [{ // nameArray模拟数据
      id: null,
      name: '全部'
    }, {
      id: 1,
      name: '测试抖音号1号'
    }, {
      id: 2,
      name: '测试抖音号2号'
    }, {
      id: 3,
      name: '测试抖音号3号'
    }],
  },
  onLoad: function (options) {
    // console.log(this.data.nameArray[this.data.index].id);
    console.log(options);
    this.setData({
      month: options.date,
      icon: options.icon,
      appName: options.appName,
      name: options.name,
      appId: options.appId,
      id: options.id,
      mid: this.data.nameArray[this.data.index].id,
    })
    this.getDateData()
    this.getName()
    this.setData({
      date: options.date,
    })
  },
  getMonthFirstLastDay(year, month) { // 获取某个月的第一天和最后一天
    var firstDay = new Date(year, month - 1, 1); // 这个月的第一天
    var currentMonth = firstDay.getMonth(); // 取得月份数
    var lastDay = new Date(firstDay.getFullYear(), currentMonth + 1, 0); // 是0而不是-1
    let start_time = utils.formatDate(firstDay, 'yyyy-mm-dd')
    let end_time = utils.formatDate(lastDay, 'yyyy-mm-dd')
    // console.log('first_last', start_time, end_time);
    this.setData({
      start_time: start_time,
      end_time: end_time,
    })
    console.log('start_end', this.data.yearMonth, this.data.start_time, this.data.end_time)
    this.getData();
  },
  getDateData: function () { // 进入页面默认年月
    console.log(this.data.month);
    var timestamp = Date.parse(new Date());
    var date1 = new Date(timestamp);
    var Y = date1.getFullYear(); // 获取年 
    var M = (date1.getMonth() + 1 < 10 ? '0' + (date1.getMonth() + 1) : date1.getMonth() + 1); // 获取月
    // var D = date1.getDate() < 10 ? '0' + date1.getDate() : date1.getDate(); // 获取日
    const yearMonth = Y + '-' + M
    this.setData({
      yearMonth: yearMonth,
      date: this.data.month,
    })
    this.getMonthFirstLastDay(Y, M)
  },
  bindDateChange: function (e) { // 更改年月日期
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      appVersion: false,
      // mid: null,
    })
    let list = e.detail.value.split('-')
    let year = list[0];
    let month = list[1];
    this.getMonthFirstLastDay(year, month)
  },
  bindNameChange: function (e) { // 更改抖音号名称
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      mid: this.data.nameArray[e.detail.value].id
    })
    console.log('mid', this.data.nameArray[e.detail.value].id);
    this.getData()
  },
  getName() { // 获取抖音号名称
    let uid = wx.getStorageSync('userid')
    wx.request({
      header: {
        'Content-Type': 'application/json'
      },
      url: baseConfig.url + 'tuiwaApp/get_md_List',
      method: 'GET',
      data: {
        uid: uid,
      },
      success: res => {
        console.log(res.data.data)
        this.setData({
          nameArray: res.data.data
        })
      }
    })
  },
  getData() { // 获取收益数据
    let that = this
    console.log(this.data.date, this.data.id, this.data.mid, this.data.appId);
    let uid = wx.getStorageSync('userid')
    // if(this.data.date !== this.data.month) {
    //   var date = this.data.month
    // } else {
    //   var date = this.data.date
    // }
    wx.request({
      header: {
        'Content-Type': 'application/json'
      },
      url: baseConfig.baseUrl + 'get_test_revenue',
      method: 'GET',
      data: {
        appId: this.data.appId,
        month: this.data.date,
        goodsId: this.data.id,
        mid: this.data.mid,
        uid: uid,
      },
      success: res => {
        console.log(res.data.data)
        if (res.data.data.sumRevenue == 0) {
          // console.log('当月无数据');
          that.setData({
            kong: true
          })
        } else {
          that.setData({
            kong: false
          })
        }
        that.setData({
          sumRevenue: res.data.data.sumRevenue,
          testRevenue: res.data.data.testRevenue,
        })
      }
    })
  },
})